package com.kuyou.ddhm_tv.DataBase;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.util.Log;


import com.kuyou.ddhm_tv.bean.BOBean;
import com.kuyou.ddhm_tv.bean.BPBean;
import com.kuyou.ddhm_tv.bean.BSBean;
import com.kuyou.ddhm_tv.bean.DeviceBean;
import com.kuyou.ddhm_tv.bean.EGBean;
import com.kuyou.ddhm_tv.bean.MeReportBean;
import com.kuyou.ddhm_tv.bean.ReportBean;
import com.kuyou.ddhm_tv.utils.BsJudge;
import com.kuyou.publiclibrary.utils.BitmapUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DataBaseHelper {
    private Context context;
    private BloodSugarDatabase bpResultDB;
    private SQLiteDatabase bpDB;

    public DataBaseHelper(Context context) {
        this.context = context;
        bpResultDB = new BloodSugarDatabase(context, "DFR.db", null, 1);

    }

    //查询血氧第一条
    public BOBean queryBOFirst() {
        BOBean boBean = null;
        bpDB = bpResultDB.getWritableDatabase();
        Cursor bpCursor = bpDB.query("BloodOxygen", null, null, null, null, null, null);

        if (bpCursor.getCount() > 0) {
            bpCursor.moveToLast();
            boBean = new BOBean(
                    bpCursor.getInt(bpCursor.getColumnIndex("id")),
                    bpCursor.getString(bpCursor.getColumnIndex("date")),
                    bpCursor.getInt(bpCursor.getColumnIndex("BO"))
                    , bpCursor.getInt(bpCursor.getColumnIndex("PR")));

        }
        bpCursor.close();
        return boBean;

    }
    //查询心电图第一条

    public EGBean queryEGFirst() {

        EGBean boBean = null;
        bpDB = bpResultDB.getWritableDatabase();
        Cursor bpCursor = bpDB.query("EGC", null, null, null, null, null, null);
        if (bpCursor.getCount() > 0) {
            bpCursor.moveToLast();
            boBean = new EGBean(
                    bpCursor.getInt(bpCursor.getColumnIndex("id")),
                    bpCursor.getString(bpCursor.getColumnIndex("date")),
                    bpCursor.getInt(bpCursor.getColumnIndex("PR"))
                    , bpCursor.getString(bpCursor.getColumnIndex("EGCData")),
                    bpCursor.getString(bpCursor.getColumnIndex("other"))
                    );


        }
        bpCursor.close();
        return boBean;

    }

    //查询血压第一条
    public BPBean queryBPFirst() {
        BPBean bpBean = null;
        bpDB = bpResultDB.getWritableDatabase();
        Cursor bpCursor = bpDB.query("Bloodpressure", null, null, null, null, null, null);
        if (bpCursor.getCount() > 0) {
            bpCursor.moveToLast();

            bpBean = new BPBean(
                    bpCursor.getInt(bpCursor.getColumnIndex("id")),
                    bpCursor.getString(bpCursor.getColumnIndex("date")),
                    bpCursor.getInt(bpCursor.getColumnIndex("height")),
                    bpCursor.getInt(bpCursor.getColumnIndex("low")),
                    bpCursor.getInt(bpCursor.getColumnIndex("bp")));


        }
        bpCursor.close();
        return bpBean;
    }

    //查询血糖第一条
    public BSBean queryBSFirst(int from) {
        String paper = "";
        switch (from) {
            case BsJudge.BS:
                paper = "'血糖试纸'";
                break;
            case BsJudge.TB:
                paper = "'总胆试纸'";
                break;
            case BsJudge.UA:
                paper = "'尿酸试纸'";
                break;
            case BsJudge.BK:
                paper = "'血酮试纸'";
                break;
        }
        BSBean bsBean = null;
        bpDB = bpResultDB.getWritableDatabase();
        Cursor bpCursor = bpDB.query("Bloodsugar", null, "paperType =" + paper, null, null, null, null);
        if (bpCursor.getCount() > 0) {
            bpCursor.moveToLast();

//                date = bpCursor.getString(bpCursor.getColumnIndex("date"));
            bsBean = new BSBean(
                    bpCursor.getInt(bpCursor.getColumnIndex("id")),
                    bpCursor.getString(bpCursor.getColumnIndex("date")),
                    bpCursor.getString(bpCursor.getColumnIndex("paperType")),
                    bpCursor.getDouble(bpCursor.getColumnIndex("bloodSugarLevels")),
                    bpCursor.getString(bpCursor.getColumnIndex("statue")));


        }
        bpCursor.close();
        return bsBean;

    }


    //查询 血氧数据列表
    public List<BOBean> queryBoList() {
        List<BOBean> list = new ArrayList<>();
        bpDB = bpResultDB.getWritableDatabase();
        Cursor bpCursor = bpDB.query("BloodOxygen", null, null, null, null, null, null);
        if (bpCursor.moveToFirst()) {
            do {
                list.add(
                        new BOBean(bpCursor.getInt(bpCursor.getColumnIndex("id")),
                                bpCursor.getString(bpCursor.getColumnIndex("date")),
                                bpCursor.getInt(bpCursor.getColumnIndex("BO"))
                                , bpCursor.getInt(bpCursor.getColumnIndex("PR"))
                        ));
            } while (bpCursor.moveToNext());
        }
        bpCursor.close();
        return list;

    }

    //插入血氧数据
    public Boolean InsertBoList(BOBean boBean) {

        bpDB = bpResultDB.getWritableDatabase();
        ContentValues values = new ContentValues();
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年M月dd日 E HH:mm");
        simpleDateFormat.format(date);
        values.put("BO", boBean.getBo());
        values.put("PR", boBean.getPr());
        values.put("date", simpleDateFormat.format(date));
        Log.d("time is", simpleDateFormat.format(date));
        Long id = bpDB.insert("BloodOxygen", null, values);
        if (id != -1) {
            Log.d("sql插入数据", "插入一条数据");
            return true;
        } else {
            Log.d("sql插入数据", "插入失败");
            return false;
        }

    }

    //插入心电图数据
    public Boolean InsertEGList(EGBean egBean) {
        bpDB = bpResultDB.getWritableDatabase();
        ContentValues values = new ContentValues();
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年M月dd日 E HH:mm");
        simpleDateFormat.format(date);
        values.put("EGCData", egBean.getDate());
        values.put("PR", egBean.getPr());
        values.put("date", simpleDateFormat.format(date));
        Log.d("time is", simpleDateFormat.format(date));
        Long id = bpDB.insert("EGC", null, values);
        if (id != -1) {
            Log.d("sql插入数据", "插入一条数据");
            return true;
        } else {
            Log.d("sql插入数据", "插入失败");
            return false;
        }

    }

    public Boolean InsertEGList(Integer hr, Bitmap bitmap, String string,String other) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年M月dd日 E HH:mm");
        Date date = new Date(System.currentTimeMillis());
        String name = date.toString();
        String path = BitmapUtils.saveImageToGallery(name, context, bitmap);
        if (path != null) {
            Log.d("插入图片", "成功");
            Log.d("插入图片", path);
            bpDB = bpResultDB.getWritableDatabase();
            ContentValues values = new ContentValues();
            simpleDateFormat.format(date);
            values.put("EGCData", path);
            values.put("PR", hr);
            values.put("other", other);
            values.put("date", simpleDateFormat.format(date));
            Log.d("time is", simpleDateFormat.format(date));
            Long id = bpDB.insert("EGC", null, values);
            if (id != -1) {
                Log.d("sql插入数据", "插入一条数据");
                return true;
            } else {
                Log.d("sql插入数据", "插入失败");
                return false;
            }
        }
        else {
            return false;
        }
    }

    //查询心电图数据
    public List<EGBean> queryEGList() {
        List<EGBean> list = new ArrayList<>();
        bpDB = bpResultDB.getWritableDatabase();
        Cursor bpCursor = bpDB.query("EGC", null, null, null, null, null, null);
        if (bpCursor.moveToFirst()) {
            do {
                list.add(new EGBean(bpCursor.getString(bpCursor.getColumnIndex("date")),
                        bpCursor.getInt(bpCursor.getColumnIndex("PR"))
                        , bpCursor.getString(bpCursor.getColumnIndex("EGCData")),
                        bpCursor.getString(bpCursor.getColumnIndex("other"))
                ));
            } while (bpCursor.moveToNext());
        }
        bpCursor.close();
        return list;

    }

    //查询设备
    public List<DeviceBean> queryDriverList() {
        List<DeviceBean> list = new ArrayList<>();
        bpDB = bpResultDB.getWritableDatabase();
        Cursor bpCursor = bpDB.query("Diver", null, null, null, null, null, null);
        if (bpCursor.moveToFirst()) {
            do {
                list.add(new DeviceBean(bpCursor.getInt(bpCursor.getColumnIndex("id")), bpCursor.getString(bpCursor.getColumnIndex("name")),
                        bpCursor.getString(bpCursor.getColumnIndex("uuid"))
                        , bpCursor.getString(bpCursor.getColumnIndex("address"))
                ));
            } while (bpCursor.moveToNext());
        }
        bpCursor.close();
        return list;
    }

    //插入设备数据
    public Boolean InsertDrive(DeviceBean boBean) {

        bpDB = bpResultDB.getWritableDatabase();
        Cursor bpCursor = bpDB.query("Diver", null, "name =" + "'" + boBean.getName() + "'", null, null, null, null);
        if (bpCursor.getCount() == 0) {
            ContentValues values = new ContentValues();
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年M月dd日 E HH:mm");
            simpleDateFormat.format(date);
            values.put("name", boBean.getName());
            values.put("uuid", boBean.getUuid());
            values.put("address", boBean.getAddress());
            Long id = bpDB.insert("Diver", null, values);
            if (id != -1) {
                bpCursor.close();
                Log.d("sql插入数据", "插入一条数据");
                return true;
            } else {
                bpCursor.close();
                Log.d("sql插入数据", "插入失败");
                return false;
            }

        } else {
            bpCursor.close();
            Log.d("sql插入数据", "数据已经存在");
            return true;
        }
    }

    //根据id删除设备数据
    public int DeleteDrive(DeviceBean bean) {
        return bpDB.delete("Diver", "id=" + "'" + String.valueOf(bean.getId()) + "'", null);
    }

    //查询评论列表
    public List<ReportBean> queryReportList(int id) {
        List<ReportBean> list = new ArrayList<>();
        bpDB = bpResultDB.getWritableDatabase();
        Cursor bpCursor = bpDB.query("Report", null, "data_id =" + String.valueOf(id), null, null, null, null);
        if (bpCursor.moveToFirst()) {
            do {
                list.add(
                        new ReportBean(bpCursor.getInt(bpCursor.getColumnIndex("id")),
                                bpCursor.getString(bpCursor.getColumnIndex("date")),
                                bpCursor.getString(bpCursor.getColumnIndex("context"))
                                , bpCursor.getInt(bpCursor.getColumnIndex("data_id"))
                        ));
            } while (bpCursor.moveToNext());
        }
        bpCursor.close();
        Log.d("查询评论列表：", list.size() + "条数据");
        return list;

    }

    //插入评论数据
    public Boolean InsertReport(ReportBean boBean) {

        bpDB = bpResultDB.getWritableDatabase();
        ContentValues values = new ContentValues();
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年M月dd日 E HH:mm");

        values.put("date", simpleDateFormat.format(date));
        values.put("context", boBean.getContext());
        values.put("data_id", boBean.getData_id());
        Long id = bpDB.insert("Report", null, values);
        if (id != -1) {

            Log.d("sql插入数据", "插入一条数据");
            return true;
        } else {

            Log.d("sql插入数据", "插入失败");
            return false;
        }

    }

    //插入血压数据
    public Boolean InsertBPList(BPBean boBean) {

        bpDB = bpResultDB.getWritableDatabase();
        ContentValues values = new ContentValues();
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年M月dd日 E HH:mm");
        simpleDateFormat.format(date);
        values.put("height", boBean.getHigh());
        values.put("low", boBean.getLow());
        values.put("bp", boBean.getBp());
        values.put("date", simpleDateFormat.format(date));
        Log.d("time is", simpleDateFormat.format(date));
        Long id = bpDB.insert("Bloodpressure", null, values);
        if (id != -1) {
            Log.d("sql插入数据", "插入一条数据");
            return true;
        } else {
            Log.d("sql插入数据", "插入失败");
            return false;
        }

    }

    //查询血压数据
    public List<BPBean> queryBPtList() {
        List<BPBean> list = new ArrayList<>();
        bpDB = bpResultDB.getWritableDatabase();
        Cursor bpCursor = bpDB.query("Bloodpressure", null, null, null, null, null, null);
        bpCursor = bpDB.query("Bloodpressure", null, null, null, null, null, null);
        if (bpCursor.moveToFirst()) {
            do {
                list.add(new BPBean(bpCursor.getString(bpCursor.getColumnIndex("date"))
                        , bpCursor.getInt(bpCursor.getColumnIndex("height")),
                        bpCursor.getInt(bpCursor.getColumnIndex("low")),
                        bpCursor.getInt(bpCursor.getColumnIndex("bp"))));
            } while (bpCursor.moveToNext());
        }
        bpCursor.close();
        Log.d("查询血压数据：", list.size() + "条数据");
        return list;

    }

    //查询血糖数据
    public List<BSBean> queryBStList() {
        List<BSBean> list = new ArrayList<>();
        bpDB = bpResultDB.getWritableDatabase();
        Cursor bpCursor = bpDB.query("Bloodsugar", null, null, null, null, null, null);
        if (bpCursor.moveToFirst()) {
            do {


                list.add( new BSBean(bpCursor.getString(bpCursor.getColumnIndex("date")),
                        bpCursor.getString(bpCursor.getColumnIndex("paperType")),
                        bpCursor.getDouble(bpCursor.getColumnIndex("bloodSugarLevels")),
                        bpCursor.getString(bpCursor.getColumnIndex("statue"))));
            } while (bpCursor.moveToNext());
        }
        bpCursor.close();
        Log.d("查询血4项数据：", list.size() + "条数据");
        return list;

    }

    //查询根据试纸类别查询数据
    public List<BSBean> queryBloodtListByPaper(int i) {
        String paper = new String();
        switch (i){
            case BsJudge.BS:
                paper = "'血糖试纸'";
                break;
            case BsJudge.TB:
                paper = "'总胆试纸'";
                break;
            case BsJudge.UA:
                paper = "'尿酸试纸'";
                break;
            case BsJudge.BK:
                paper = "'血酮试纸'";
                break;
        }
        List<BSBean> list = new ArrayList<>();
        bpDB = bpResultDB.getWritableDatabase();
        Cursor bpCursor = bpDB.query("Bloodsugar", null, "paperType ="+paper, null, null, null, null);
        if (bpCursor.moveToFirst()) {
            do {


                list.add( new BSBean(bpCursor.getString(bpCursor.getColumnIndex("date")),
                        bpCursor.getString(bpCursor.getColumnIndex("paperType")),
                        bpCursor.getDouble(bpCursor.getColumnIndex("bloodSugarLevels")),
                        bpCursor.getString(bpCursor.getColumnIndex("statue"))));
            } while (bpCursor.moveToNext());
        }
        bpCursor.close();
        Log.d("查询根据试纸类别查询数据：", list.size() + "条数据");
        return list;

    }

    public Boolean InsertMEList(MeReportBean boBean) {

        bpDB = bpResultDB.getWritableDatabase();
        ContentValues values = new ContentValues();
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年M月dd日 E HH:mm");
        simpleDateFormat.format(date);
        values.put("time", boBean.getTime());
        values.put("report", boBean.getReport());
        values.put("high", boBean.getHigh());
        values.put("low", boBean.getLow());
        values.put("bpm", boBean.getBpm());
        values.put("bo", boBean.getBo());
        values.put("bp", boBean.getBp());
        values.put("pr", boBean.getPr());
        values.put("bs", boBean.getBs());
        values.put("dgc", boBean.getDgc());
        values.put("ns", boBean.getNs());
        values.put("xt", boBean.getXt());
        values.put("egc", boBean.getEgc());
        values.put("bsState", boBean.getBsState());
        values.put("dgcState", boBean.getDgcState());
        values.put("nsState", boBean.getNsState());
        values.put("xtState", boBean.getXtState());
        Log.d("time is", simpleDateFormat.format(date));
        Long id = bpDB.insert("ME", null, values);
        if (id != -1) {
            Log.d("sql插入数据", "插入一条数据");
            return true;
        } else {
            Log.d("sql插入数据", "插入失败");
            return false;
        }

    }


    public List<MeReportBean> queryMEtList() {
        List<MeReportBean> list = new ArrayList<>();
        bpDB = bpResultDB.getWritableDatabase();
        Cursor bpCursor = bpDB.query("ME", null, null, null, null, null, null);
        if (bpCursor.moveToFirst()) {
            do {


                list.add( new MeReportBean(bpCursor.getString(bpCursor.getColumnIndex("time")),
                        bpCursor.getString(bpCursor.getColumnIndex("report")),
                        (float)bpCursor.getDouble(bpCursor.getColumnIndex("high")),
                        (float)bpCursor.getDouble(bpCursor.getColumnIndex("low")),
                        bpCursor.getInt(bpCursor.getColumnIndex("bpm")),
                        bpCursor.getInt(bpCursor.getColumnIndex("bo")),
                        bpCursor.getInt(bpCursor.getColumnIndex("bp")),
                        bpCursor.getInt(bpCursor.getColumnIndex("pr")),
                        (float)bpCursor.getDouble(bpCursor.getColumnIndex("bs")),
                        bpCursor.getString(bpCursor.getColumnIndex("bsState")),
                        (float)bpCursor.getDouble(bpCursor.getColumnIndex("dgc")),
                        bpCursor.getString(bpCursor.getColumnIndex("dgcState")),
                        (float)bpCursor.getDouble(bpCursor.getColumnIndex("ns")),
                        bpCursor.getString(bpCursor.getColumnIndex("nsState")),
                        (float)bpCursor.getDouble(bpCursor.getColumnIndex("xt")),
                        bpCursor.getString(bpCursor.getColumnIndex("xtState")),
                        bpCursor.getString(bpCursor.getColumnIndex("egc"))));
            } while (bpCursor.moveToNext());
        }
        bpCursor.close();
        Log.d("查询血4项数据：", list.size() + "条数据");
        return list;

    }


}
